package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-11
 * Time: 12:33
 */

class Money implements Cloneable{
    public double m = 66.66;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public int age;
    Money money = new Money();

    public void eat(){
        System.out.println("吃！");
    }

    @Override
    public String toString() {
        return "demo1.Person{" +
                "age=" + age +
                '}';
    }

    //重写 clone() 方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person) super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;
    }
}

public class TestDemo1  {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        Person person2 = (Person) person1.clone();
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
        System.out.println("================");
        person2.money.m = 33.33;
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
    }
    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        person1.age = 99;
        //clone() 默认是 Object 类，所以将其进行强制类型转换
        Person person2 = (Person) person1.clone();
        System.out.println(person2.age);
        System.out.println("====================");
        person2.age=50;
        System.out.println(person1.age);
        System.out.println(person2.age);
    }
}
